package com.enniswu.weitter.data.remote;

import android.support.annotation.Nullable;

import com.enniswu.weitter.base.BaseModel;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.JsonBean;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.network.HttpClient;
import com.google.gson.Gson;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.enniswu.weitter.entity.JsonBean.TypeStore.objType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.postType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.strListType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.strType;
import static com.enniswu.weitter.entity.JsonBean.TypeStore.userType;

public class FileLoader extends BaseModel {

    private static final String KEY_IMAGE = "image";
    private static final String KEY_ALBUM = "album";

    public interface ImageCallback extends Callback<User> {
        void onLoadSuccess(@Nullable String url);
    }

    public interface AlbumCallback extends Callback<Post> {
        void onLoadSuccess(@Nullable List<String> url);
    }

    public void uploadImage(int id, File image, final Callback<Object> callback) {
        Map<String, String> params = new HashMap<>(2, 1);
        params.put("id", String.valueOf(id));
        params.put("type", String.valueOf(1));
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.UPLOAD_IMAGE, params, KEY_IMAGE,
                Collections.singletonList(image), new HttpCallbackImpl<>(callback, objType));
    }

    public void downloadImage(int id, final ImageCallback callback) {
        Map<String, String> params = new HashMap<>(2, 1);
        params.put("id", String.valueOf(id));
        params.put("type", String.valueOf(1));
        HttpClient.getInstance().post(null, HttpClient.URLStore.DOWNLOAD_IMAGE,
                params, new HttpCallbackImpl<User>(callback, userType) {
                    @Override
                    public void onSuccess(String json) {
                        JsonBean<String> jsonBean = new Gson().fromJson(json, strType);
                        callback.onLoadSuccess(jsonBean.getT());
                    }
                });
    }

    public void uploadAlbum(int id, List<File> images, final Callback<Object> callback) {
        Map<String, String> params = new HashMap<>(3, 1);
        params.put("id", String.valueOf(id));
        params.put("userType", String.valueOf(1));
        params.put("albumType", String.valueOf(0));
        HttpClient.getInstance().post(SPSupport.getInstance().getSessionId(),
                HttpClient.URLStore.UPLOAD_ALBUM, params,
                KEY_ALBUM, images, new HttpCallbackImpl<>(callback, objType));
    }

    public void downloadAlbum(int id, final AlbumCallback callback) {
        Map<String, String> params = new HashMap<>(3, 1);
        params.put("id", String.valueOf(id));
        params.put("userType", String.valueOf(1));
        params.put("albumType", String.valueOf(0));
        HttpClient.getInstance().post(null, HttpClient.URLStore.DOWNLOAD_ALBUM,
                params, new HttpCallbackImpl<Post>(callback, postType) {
                    @Override
                    public void onSuccess(String json) {
                        JsonBean<List<String>> jsonBean = new Gson().fromJson(json, strListType);
                        callback.onLoadSuccess(jsonBean.getT());
                    }
                });
    }
}
